﻿using System;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfShopView : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                //this.SetContrlValue("txtId", GetFormParam("ID"));
                //ShowData(GetFormParam("ID"));
                ShowData(this.Request.QueryString.Get("ID"));

                loadHeader(this.Request.QueryString.Get("ID"));
                loadContentTop(this.Request.QueryString.Get("ID"));
                loadGoodslist(this.Request.QueryString.Get("ID"));
            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
            }
        }

        public string strDealerId = "";
        public DataRow drDealer = null;
        private void ShowData(string strId)
        {
            if (string.IsNullOrEmpty(strId))
            {
                this.ToErrPage("店铺信息未找到！");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo())
            {
                System.Data.DataSet ds = oCDbAccess.SelectById(strId);
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    this.ToErrPage("店铺信息未找到！");
                    return;
                }
                else
                {
                    strDealerId = strId;
                    this.ExecJScript("$('#txtId').val('" + strDealerId + "');");

                    drDealer = ds.Tables[0].Rows[0];
                }
            }
        }

        private void loadGoodslist(string _strDealerId)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo odataobj = new AppLogic.DataAlc.CBizShpGoodsInfo())
            {
                DataSet ds = odataobj.SelectGoodsListForShopVew(_strDealerId);

                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], ""
                    , "col_id,col_nme,colspan_num,is_show,col_title,shop_id,col_num,more_link", 1, 1000, "col_num ASC,goods_num asc");
//                this.ExecJScript(@"debugger;
//                "); 
                this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                doBoundGoodsList( data,'" + drDealer["GOODS_LIST_TEMPLATE_URL"].ToString() + "');"); 
            }
        }


        private void loadContentTop(string _strDealerId)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpCommercialHeadImg odataobj = new AppLogic.DataAlc.CBizShpCommercialHeadImg())
            {
                DataSet ds = odataobj.SelectByDealerId(_strDealerId);

            string strjsontem1 =  msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "SEQ_NUM ASC");
                this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                doBoundContentTop( data,'" + drDealer["CONTENT_TOP_TEMPLATE_URL"].ToString() + "');");
            }
        }


        private void loadHeader(string _strDealerId)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpGoodsInfo odataobj = new AppLogic.DataAlc.CBizShpGoodsInfo())
            //{
            //    DataSet ds1 = odataobj.DataObj.QueryBySQL("SELECT CT_LEVEL_ONE_ID,CT_LEVEL_ONE_NME FROM SHP_COMMERCIAL_GOODS_LEVEL_ONE WHERE COMM_TENANT_ID='" + _strDealerId + "'");
            //    DataSet ds2 = odataobj.DataObj.QueryBySQL("SELECT CT_LEVEL_TWO_ID,CT_LEVEL_TWO_NME,CT_LEVEL_ONE_ID FROM SHP_COMMERCIAL_GOODS_LEVEL_TWO WHERE COMM_TENANT_ID='" + _strDealerId + "'");
            //    DataSet ds3 = odataobj.DataObj.QueryBySQL("SELECT CT_LEVEL_THREE_ID,CT_LEVEL_THREE_NME,CT_LEVEL_TWO_ID FROM SHP_COMMERCIAL_GOODS_LEVEL_THREE WHERE COMM_TENANT_ID='" + _strDealerId + "'");
            //    DataSet dsimg = odataobj.DataObj.QueryBySQL("SELECT SEQ_NUM,IMG_URL,BG_COLOR,LINK_URL FROM SHP_COMMERCIAL_HEAD_IMG WHERE COMM_TENANT_ID='" + _strDealerId + "' ORDER BY SEQ_NUM ASC ");

            //    System.Text.StringBuilder sbRet = new System.Text.StringBuilder();
            //    sbRet.Append("{");
            //    sbRet.Append("\"shop_id\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(_strDealerId) + "\"");
            //    sbRet.Append(",\"shop_name\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(drDealer["COMM_TENANT_NME"].ToString()) + "\"");
            //    sbRet.Append(",\"logo_url\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(drDealer["LOGO_IMG_URL"].ToString()) + "\"");
            //    sbRet.Append(",\"head_img_url\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(drDealer["DEFAULT_HEADER_IMG_URL"].ToString()) + "\"");
            //    sbRet.Append(",\"head_bg_color\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(drDealer["DEFAULT_HEADER_BG_COLOR"].ToString()) + "\"");

            //    sbRet.Append(",\"img_count\":" + dsimg.Tables[0].Rows.Count.ToString() + ",imgrows:[");
            //    for (int a = 0; a < dsimg.Tables[0].Rows.Count; a++)
            //    {
            //        if (a > 0)
            //        {
            //            sbRet.Append(",");
            //        }
            //        sbRet.Append("{");
            //        sbRet.Append("\"seq_num\":" + ds1.Tables[0].Rows[a]["SEQ_NUM"].ToString() + "");
            //        sbRet.Append(",\"img_url\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dsimg.Tables[0].Rows[a]["IMG_URL"].ToString()) + "\"");
            //        sbRet.Append(",\"bg_color\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dsimg.Tables[0].Rows[a]["BG_COLOR"].ToString()) + "\"");
            //        sbRet.Append(",\"link_url\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(dsimg.Tables[0].Rows[a]["LINK_URL"].ToString()) + "\"");

            //        sbRet.Append("}");
            //    }
            //    sbRet.Append("]");


            //    sbRet.Append(",\"r_count\":" + ds1.Tables[0].Rows.Count.ToString() + ",rows:[");
            //    for (int a = 0; a < ds1.Tables[0].Rows.Count; a++)
            //    {
            //        if (a > 0)
            //        {
            //            sbRet.Append(",");
            //        }
            //        sbRet.Append("{");
            //        sbRet.Append("\"ID\":\"" + ds1.Tables[0].Rows[a]["CT_LEVEL_ONE_ID"].ToString() + "\"");
            //        sbRet.Append(",\"NAME\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(ds1.Tables[0].Rows[a]["CT_LEVEL_ONE_NME"].ToString()) + "\"");

            //        DataRow[] arrrow1 = ds2.Tables[0].Select("CT_LEVEL_ONE_ID='" + ds1.Tables[0].Rows[a]["CT_LEVEL_ONE_ID"].ToString() + "'");
            //        sbRet.Append(",\"r_count\":" + arrrow1.Length.ToString() + ",rows:[");
            //        for (int b = 0; b < arrrow1.Length; b++)
            //        {
            //            if (b > 0)
            //            {
            //                sbRet.Append(",");
            //            }
            //            sbRet.Append("{");
            //            sbRet.Append("\"ID\":\"" + arrrow1[b]["CT_LEVEL_TWO_ID"].ToString() + "\"");
            //            sbRet.Append(",\"NAME\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(arrrow1[b]["CT_LEVEL_TWO_NME"].ToString()) + "\"");

            //            DataRow[] arrrow2 = ds3.Tables[0].Select("CT_LEVEL_TWO_ID='" + arrrow1[b]["CT_LEVEL_TWO_ID"].ToString() + "'");
            //            sbRet.Append(",\"r_count\":" + arrrow2.Length.ToString() + ",rows:[");
            //            for (int c = 0; c < arrrow2.Length; c++)
            //            {
            //                if (c > 0)
            //                {
            //                    sbRet.Append(",");
            //                }
            //                sbRet.Append("{");
            //                sbRet.Append("\"ID\":\"" + arrrow2[c]["CT_LEVEL_THREE_ID"].ToString() + "\"");
            //                sbRet.Append(",\"NAME\":\"" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(arrrow2[c]["CT_LEVEL_THREE_NME"].ToString()) + "\"");

            //                sbRet.Append(",\"r_count\":0,rows:[");

            //                sbRet.Append("]");


            //                sbRet.Append("}");
            //            }


            //            sbRet.Append("]");


            //            sbRet.Append("}");
            //        }

            //        sbRet.Append("]");


            //        sbRet.Append("}");
            //    }
            //    sbRet.Append("]}");



                using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo objtem1 = new AppLogic.DataAlc.CBizShpCommercialTenantInfo())
                {
                    string strjsontem1 = objtem1.GetHeadJsonData(_strDealerId,drDealer);// msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "DATA_TYPE,DATA_TYPE_DESC,DATA_SEQ_NUM,CHILD_LINKS", 1, 1000, "DATA_SEQ_NUM ASC");
                    //this.ExecJScript("debugger;");
                    this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                doBoundHeader( data,'" + drDealer["HEADER_TEMPLATE_URL"].ToString() + "');");
                }
            //}
        }
    }
}